package com.example.smarterhouse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class Functions extends Activity {
	private String name;
	private TextView dev;
	private Switch sw;
	private Controller controll;
	private String status;
	private String pos;
	private String togg;
	private TextView noTogg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_functions);
		controll = Controller.getInstance();
		controll.setCurrrentClass(Functions.class);
		controll.setCon(getApplicationContext());
		Log.d("DEVICE", "FUNCTIONS");
		Intent myIntent = getIntent();
		noTogg = (TextView) findViewById(R.id.noTog);
		sw = (Switch) findViewById(R.id.switchen);
		noTogg.setVisibility(View.GONE);
		sw.setVisibility(View.GONE);
		String splits = myIntent.getExtras().getString("getDevice_");
		String[] device = splits.split("_");
		Log.d("Length", splits);
		name = device[0];
		pos = device[1];
		status = device[2];
		togg = device [3];
		if(togg.equals("onTog")){
			sw.setVisibility(View.VISIBLE);
		}else if (togg.equals("offTog")){
			noTogg.setVisibility(View.VISIBLE);
			noTogg.setText(device[2] +" Degrees Celsius");
		}
		Log.d("name", name);
		Log.d("status", status);
		Log.d("pos", pos);
		if (status.equals("1")) {
			sw.setChecked(true);
		} else if (status.equals("0")) {
			sw.setChecked(false);
		}

		dev = (TextView) findViewById(R.id.device);
		dev.setText(name);
		sw.setOnCheckedChangeListener(null);
		sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			//Modified to work with the communication protocol by Henrik, Sanja & Valdemar
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				String toServer;
				String toggle ="";
				toServer = "checkDevice:" + pos + ":";
				String status ="";
				if (controll.callAsync("checkDevice_" + pos + "_")) {
					String fromServer = controll.getInputFromServer();
					controll.setInputFromServer("");
					Log.d("checkDEVICE2", fromServer);
					String[] device = fromServer.split("_");
					 status = device[1];
	
					
					Log.d("status second check", status);
					if (status.equals("1")) {
						
						sw.setChecked(true);
					} else {
						sw.setChecked(false);
					}
				}
				if (status.equals("0")) {
					Log.d("SEND TOGGLE FALSE", status);
					toggle = "toggleDevice_" + pos + "_1_";
					Log.d("TOOGLE FALSE", toggle);
					Boolean set =controll.callAsync(toggle);
					Log.d("SET FALSE", set.toString());
					if (!set) {
						Toast toast = Toast.makeText(getApplicationContext(),
								"Failed to change status for " + name
										+ " please try again",
								Toast.LENGTH_LONG);
						toast.show();
					} 
					sw.setChecked(true);
					}else if (status.equals("1")){
						Log.d("SEND TOGGLE TRUE", status);
						toggle = "toggleDevice_" + pos + "_0_";
						Log.d("TOOGLE TRUE", toggle);
						Boolean set2 = controll.callAsync(toggle);
						Log.d("SET2 TRUE", set2.toString());
						if (!set2) {
							Toast toast = Toast.makeText(getApplicationContext(),
									"Failed to change status for " + name
											+ " please try again",
									Toast.LENGTH_LONG);
							toast.show();
						}
						sw.setChecked(false);


				Log.d("PUSSYCAT", toServer);
					}
				
			}
		

	});

}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.room_and_device_list, menu);
	    
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		Log.d("MENU", String.valueOf(item.getItemId()));
	    switch (item.getItemId()) {
	        case R.id.logoutButton:
	        	Log.d("LOGOUT", "FUCK");
	            controll.callAsync("logout_");
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);

	    }
	}
	
//	@Override
//	protected void onPause() {
//		controll.callAsync("getRooms_");
//		finish();
//	}
	
}
		
